package com.lead.security.encryption.SHA1;

import com.lead.security.utils.HexUtil;

import java.security.MessageDigest;

/**
 * @ClassName: MessageDigestUtil
 * @Description:
 * @Author: zhouzhiguo
 * @Date: 2024/5/27 15:57
 * @Version: 5.0.6
 * 上海利得金融科技集团版权所有.
 */

public class MessageDigestUtil {
    private static final String TAG = MessageDigestUtil.class.getSimpleName();

    private static final String TYPE_MD5 = "MD5";
    private static final String TYPE_SHA1 = "SHA1";

    /**
     * 计算该字符串的MD5
     *
     * @param msg
     * @return
     */
    public static String toMd5(String msg)  throws Exception {
        return toDigest(TYPE_MD5, msg);
    }

    /**
     * 计算该字符串的SHA1
     *
     * @param msg
     * @return
     */
    public static String toSha1(String msg) throws Exception {
        return toDigest(TYPE_SHA1, msg);
    }

    private static String toDigest(String type, String msg) throws Exception {
        if (msg == null || msg.trim().isEmpty()) {
            return "";
        }
        String res = "";
        MessageDigest md = MessageDigest.getInstance(type);
        byte[] b = md.digest(msg.getBytes("utf-8"));
        res = HexUtil.toHex(b);
        return res;
    }



}
